package com.dd.controller;


import com.dd.common.aop.annation.Login;
import com.dd.pojo.query.UserFileQuery;
import com.dd.pojo.query.DeleteFileQuery;
import com.dd.pojo.query.DeleteFolderQuery;
import com.dd.pojo.query.FolderQuery;
import com.dd.common.response.Result;
import com.dd.pojo.entry.Folder;
import com.dd.pojo.entry.UserFoldFile;
import com.dd.pojo.vo.MergerFileVO;
import com.dd.service.FileService;
import com.dd.common.utils.HttpUtils;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Api(tags = "文件接口")
public class FileController {

    private final FileService fileService;




    @PostMapping("/check_file_upload_status")
    @ApiOperation("检查文件上传状态")
    @Login
   public Result<Integer>checkFile(@RequestBody UserFileQuery checks, HttpServletRequest request){
       checks.setUserId(HttpUtils.anasyUserId(request));
        int status = fileService.checkFileUplaodStatus(checks);
        return  Result.success(status);
    }



    @PostMapping("/mkdir_file")
    @ApiOperation("创建文件夹")
    @Login
    public Result<?> mkdirFolder(@RequestBody FolderQuery form, HttpServletRequest request){
        String id = HttpUtils.anasyUserId(request);
        fileService.mkdirFolDer(form.getFolderName(),id,form.getParentId());
        return Result.success();
    }

    @PostMapping("/upload_file")
    @ApiOperation("上传小文件")
    @Login
    public Result<?> uploadFile(@ApiParam(name = "file",value = "file")@RequestParam("file")MultipartFile file,
                                @ApiParam(name = "fileMd5",value = "fileMd5")@RequestParam("fileMd5")String fileMd5,
                                @ApiParam(name = "folderId",value = "folderId")@RequestParam("folderId")String folderId,
                                HttpServletRequest request){
        String id = HttpUtils.anasyUserId(request);
        fileService.upladFile(file,fileMd5,id,folderId);
        return Result.success();}




    @PostMapping("/find_file_data")
    @ApiOperation("查询文件/文件夹数据")
    @Login
    public Result<?> findFileData(@RequestBody FolderQuery form, HttpServletRequest request){
        String id = HttpUtils.anasyUserId(request);
        List<UserFoldFile> files = fileService.findFiles(id, form.getFolderId());
        List<Folder> folders = fileService.findOneTypeFolder(form.getParentId(), id);
        Map<String, Object> map = new HashMap<>();
        map.put("folders",folders);
        map.put("files",files);
        return  Result.success(map);
    }

    @Login
    @GetMapping("/find_folder_info/{folderId}")
    @ApiOperation("查询文件夹信息")
    @ApiImplicitParam(name = "folderId",value ="文件夹ID",required = true,dataTypeClass = String.class)
    public Result<Folder>findFolderInfo(@PathVariable("folderId") String folderId,HttpServletRequest request){
        Folder folderInfo = fileService.findFolderInfo(folderId, HttpUtils.anasyUserId(request));
        return Result.success(folderInfo);
    }


    @DeleteMapping("/delete_file")
    @ApiOperation("删除文件")
    @Login
    public Result<?> deleteFile(@RequestBody DeleteFileQuery form, HttpServletRequest request){
        form.setUserId(HttpUtils.anasyUserId(request));
        fileService.deleteFile(form);
        return  Result.success();
    }


    @DeleteMapping("/delete_folder")
    @ApiOperation("删除文件夹")
    @Login
    public Result<?> editFolder(@RequestBody DeleteFolderQuery form){
        fileService.deleteFolder(form);
        return  Result.success();
    }

    @PostMapping("/uploadFileChunk")
    @ApiOperation(value = "上传分片文件")
    public Result<?>uploadFileChunk(@ApiParam(name = "file",value = "分片文件") @RequestParam("file")MultipartFile file,
                                    @ApiParam(name = "fileMd5",value = "文件MD5")@RequestParam("fileMd5")String fileMd5,
                                    @ApiParam(name = "fileIndex",value = "分片文件数组下标")@RequestParam("fileIndex")String fileIndex) throws ExecutionException, InterruptedException {

       fileService.uploadChunk(fileIndex, fileMd5, file);

        return Result.success(fileIndex);
    }

    @PostMapping("/findChunkList")
    @ApiOperation(value = "获取已经上传的分片文件数组")
    public Result<List<Integer>> findChunkList( @ApiParam(name = "fileMd5",value = "文件MD5")@RequestParam("fileMd5")String fileMd5){

        List<Integer> list = fileService.findChunkd(fileMd5);
        return Result.success(list);
    }


    @PostMapping("/merger")
    @ApiOperation(value = "合并分片")
    public Result<?> merger(@RequestBody MergerFileVO vo,HttpServletRequest request){
        String uid=HttpUtils.anasyUserId(request);
        fileService.merger(vo,uid);
        return Result.success();
    }

    @GetMapping("/test/{index}")
    @ApiOperation("测试")
    public  Result<Integer> test(@PathVariable("index")Integer index) throws InterruptedException, ExecutionException {

        Future<Integer> future = fileService.uploadFile(index);
        return Result.success(future.get());
    }










}
